package com.example.triprec.ui.activity;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.triprec.R;
import com.example.triprec.ui.adapter.MainViewPagerAdapter;
import com.example.triprec.ui.fragment.HomeFragment;
import com.example.triprec.ui.fragment.MineFragment;
import com.example.triprec.ui.fragment.TripFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager2 vpMain;
    private BottomNavigationView navBottom;
    private List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        vpMain = findViewById(R.id.vp_main);
        navBottom = findViewById(R.id.nav_bottom);
    }

    private void initData() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new TripFragment());
        fragmentList.add(new MineFragment());

        MainViewPagerAdapter adapter = new MainViewPagerAdapter(this, fragmentList);
        vpMain.setAdapter(adapter);
    }

    private void initListener() {
        // 设置ViewPager2滑动监听
        vpMain.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        navBottom.setSelectedItemId(R.id.nav_home);
                        break;
                    case 1:
                        navBottom.setSelectedItemId(R.id.nav_trip);
                        break;
                    case 2:
                        navBottom.setSelectedItemId(R.id.nav_mine);
                        break;
                }
            }
        });

        // 设置底部导航选择监听
        navBottom.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.nav_home) {
                    vpMain.setCurrentItem(0, false);
                    return true;
                } else if (id == R.id.nav_trip) {
                    vpMain.setCurrentItem(1, false);
                    return true;
                } else if (id == R.id.nav_mine) {
                    vpMain.setCurrentItem(2, false);
                    return true;
                }
                return false;
            }
        });
    }
}